﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Direct.Security.Model;
using Direct.EntityStore;
using Direct.Core;

namespace Direct.Security.Impl
{
    class UserImpl : SecurityObjectImpl, IUser
    {
        /*
         * ExtFld1 = Email
         * ExtFld2 = password hash
         * ExtFld3 = não usado
         */

        public UserImpl(ISchema schema, SecObjEntity model)
            : base(model, SecObjType.User)
        {
            Schema = schema;
            Login = model.ObjectId;
            Name = model.Name;
            Email = model.ExtFld1;
            Active = model.Active;
            PasswordHash = model.ExtFld2;
        }

        public ISchema Schema { get; private set; }
        public string Login { get; private set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public bool Active { get; set; }
        public String PasswordHash { get; private set; }

        public bool IsValidPassword(string password)
        {
            if (PasswordHash.IsEmpty())
                return password.IsEmpty();
            var token = new Token(PasswordHash);
            return token.Get("objId") == ObjId && token.Get("pwd") == password;
        }
    }
}
